//
//  MyStandingsViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/21.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "MyStandingsViewController.h"
#import "StandingsFirstTableViewCell.h"
#import "StandingSecondsTableViewCell.h"

#define Margin 8

@interface MyStandingsViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *MyStandingsTable;

@property (nonatomic,strong)NSArray *allStandingArray;

//@property (nonatomic,strong)UILabel *AllStandings;
@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

static NSString *const firstReuseIdentifier = @"firstReuseIdentifier";

@implementation MyStandingsViewController

- (NSArray *)allStandingArray {
    if (!_allStandingArray) {
        _allStandingArray = [NSArray array];
    }
    return _allStandingArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"积分详情";
    
    NSLog(@"%f",CGRectGetMaxY(self.navigationController.navigationBar.frame));
    
    [self getIntegralDataSources];
    
    [self loadHeaderImageView];
    
    [self loadMyStandingsTableView];
    
    UIButton *backButton = [[UIButton alloc] init];
    [backButton setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(regardingBackButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15);
        make.top.equalTo(self.view.mas_top).offset(40);
        make.width.height.mas_equalTo(20);
    }];
}

- (void)regardingBackButtonDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadMyStandingsTableView {
    self.MyStandingsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.3, WIDTH, self.view.Height * 0.7) style:UITableViewStylePlain];
    self.MyStandingsTable.delegate = self;
    self.MyStandingsTable.dataSource = self;
    [self.view addSubview:self.MyStandingsTable];
}

- (void)loadHeaderImageView {
    UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.3)];
    headerImageView.image = [UIImage imageNamed:@"jifenxiangqing_1"];
    [self.view addSubview:headerImageView];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont systemFontOfSize:19];
    nameLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(23.0)];
    nameLabel.textColor = [UIColor whiteColor];
    NSString *nameString = USERNAME;
    nameLabel.text = [NSString stringWithFormat:@"用户名: %@",nameString];
    [headerImageView addSubview:nameLabel];
    [self.view addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).with.offset(headerImageView.Height/2);
//        make.centerY.equalTo(self.view.mas_centerY);
        make.width.mas_equalTo(self.view.width);
        make.height.mas_equalTo(40);
    }];
    
    
//    我的积分头部总积分的显示
    /*
     
     UILabel *AllStandings = [[UILabel alloc] init];
     AllStandings.text = @"0分";
     AllStandings.textColor = [UIColor whiteColor];
     AllStandings.font = [UIFont systemFontOfSize:18];
     AllStandings.textAlignment = NSTextAlignmentCenter;
     AllStandings.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(21.0)];
     [headerImageView addSubview:AllStandings];
     self.AllStandings = AllStandings;
     [self.view addSubview:AllStandings];
     
     [AllStandings mas_makeConstraints:^(MASConstraintMaker *make) {
     make.centerX.equalTo(self.view.mas_centerX).with.offset(Margin * 3);
     make.top.equalTo(nameLabel.mas_bottom).with.offset(Margin * 2);
     make.width.mas_equalTo(self.view.width * 0.2);
     make.height.mas_equalTo(30);
     }];
     
     UIImageView *coinImageView = [[UIImageView alloc] init];
     coinImageView.image = [UIImage imageNamed:@"jifenxiangqing_0"];
     [headerImageView addSubview:coinImageView];
     [self.view addSubview:coinImageView];
     
     [coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.right.equalTo(AllStandings.mas_left).with.offset(-Margin);
     make.centerY.equalTo(AllStandings.mas_centerY);
     make.width.mas_equalTo(25);
     make.height.mas_equalTo(25);
     }];

     */
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allStandingArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        StandingsFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstReuseIdentifier];
        
        if (cell == nil) {
            cell = [[StandingsFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:firstReuseIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
//        cell.textLabel.text = @"test";
        
        return cell;
    } else {
        StandingSecondsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        
        if (cell == nil) {
            cell = [[StandingSecondsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        NSInteger place = indexPath.row - 1;
        
        cell.todayStandings.text = [self.allStandingArray[place] objectForKey:@"meiri"];
        cell.timeLabel.text = [self.allStandingArray[place] objectForKey:@"addtime"];
        cell.allStanding.text = [self.allStandingArray[place] objectForKey:@"zongjifen"];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 64;
    } else {
        return 44;
    }
}

- (void)getIntegralDataSources {
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":USERNAME} AndMenthodName:@"AppUser/Integ" completeBlock:^(id object) {
        
        self.allStandingArray = object[@"data"];
        
//        self.AllStandings.text = [self.allStandingArray.firstObject objectForKey:@"zongjifen"];
        
        [self.MyStandingsTable reloadData];
    }];
}

@end
